﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace blackhouse.Translator.Business.GlobalResources {
    internal class ResxResourceEditor : IResourceEditor {

        #region Fields

        private bool clearDataWhenOpen;
        private FileInfo resxFile;
        private FileStream resxFileStream;
        private XmlDocument resxDocument;

        #endregion

        #region Constructors

        public ResxResourceEditor(FileInfo defaultResourceFile, FileInfo destinationResourceFile) {
            if (!destinationResourceFile.Exists) {
                destinationResourceFile = defaultResourceFile.CopyTo(destinationResourceFile.FullName);
                this.clearDataWhenOpen = true;
            }
            this.resxFile = destinationResourceFile;
            this.OpenResxFileForRead();
        }

        #endregion

        #region Methods

        private void OpenResxFileForRead() {
            using (this.resxFileStream = this.resxFile.Open(FileMode.Open)) {
                this.resxDocument = new XmlDocument();
                this.resxDocument.Load(this.resxFileStream);
            }
            if (this.clearDataWhenOpen)
                this.ClearDocumentValues();
        }

        private void ClearDocumentValues() {
            XmlNode rootNode = this.resxDocument.SelectSingleNode("root");
            XmlNodeList dataNodes = this.resxDocument.SelectNodes("root/data");
            if (rootNode == null || dataNodes == null) return;
            foreach (XmlNode dataNode in dataNodes) {
                rootNode.RemoveChild(dataNode);
                //foreach (XmlNode childDataNode in dataNode.ChildNodes) {
                //    switch (childDataNode.Name) {
                //        case "value":
                //            childDataNode.InnerText = String.Empty;
                //            break;
                //        case "comment":
                //            dataNode.RemoveChild(childDataNode);
                //            break;
                //    }
                //}
            }
        }

        public void SetTranslatedText(string resourceKey, string text) {
            XmlNode dataNode = this.resxDocument.SelectSingleNode("root/data[@name='" + resourceKey + "']");
            if (dataNode == null)
                dataNode = this.CreateDataNode(resourceKey);
            XmlNode dataValueNode = dataNode.SelectSingleNode("value");
            dataValueNode.InnerText = text;
        }

        private XmlNode CreateDataNode(string resourceKey) {
            XmlNode dataNode = this.resxDocument.CreateElement("data");
            XmlAttribute keyAttribute = this.resxDocument.CreateAttribute("name");
            XmlAttribute preserveAttribute = this.resxDocument.CreateAttribute("xml:space");
            keyAttribute.Value = resourceKey;
            preserveAttribute.Value = "preserve";
            dataNode.Attributes.Append(keyAttribute);
            dataNode.Attributes.Append(preserveAttribute);
            XmlNode valueNode = this.resxDocument.CreateElement("value");
            valueNode.InnerText = String.Empty;
            dataNode.AppendChild(valueNode);
            this.resxDocument.SelectSingleNode("root").AppendChild(dataNode);
            return dataNode;
        }

        public void Save() {
            using (this.resxFileStream = this.resxFile.Open(FileMode.Truncate)) {
                this.resxDocument.Save(this.resxFileStream);
            }
        }

        public void Dispose() {
            this.resxFileStream.Dispose();
        }

        #endregion

    }
}
